<template>
  <el-dialog
    :title="!dataForm.name ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="文件页数" prop="page">
      <el-input v-model="dataForm.page" placeholder="文件页数"></el-input>
    </el-form-item>
    <el-form-item label="打印份数" prop="copies">
      <el-input v-model="dataForm.copies" placeholder="打印份数"></el-input>
    </el-form-item>
    <el-form-item label="0单面1双面" prop="type">
      <el-input v-model="dataForm.type" placeholder="0单面1双面"></el-input>
    </el-form-item>
    <el-form-item label="" prop="prices">
      <el-input v-model="dataForm.prices" placeholder=""></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          name: 0,
          page: '',
          copies: '',
          type: '',
          prices: ''
        },
        dataRule: {
          page: [
            { required: true, message: '文件页数不能为空', trigger: 'blur' }
          ],
          copies: [
            { required: true, message: '打印份数不能为空', trigger: 'blur' }
          ],
          type: [
            { required: true, message: '0单面1双面不能为空', trigger: 'blur' }
          ],
          prices: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.name = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.name) {
            this.$http({
              url: this.$http.adornUrl(`/order/file/info/${this.dataForm.name}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.page = data.file.page
                this.dataForm.copies = data.file.copies
                this.dataForm.type = data.file.type
                this.dataForm.prices = data.file.prices
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/order/file/${!this.dataForm.name ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'name': this.dataForm.name || undefined,
                'page': this.dataForm.page,
                'copies': this.dataForm.copies,
                'type': this.dataForm.type,
                'prices': this.dataForm.prices
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
